package com.imooc.distributedemo.controller;

import com.imooc.distributedemo.common.CommonResult;
import com.imooc.distributedemo.entity.Order;
import com.imooc.distributedemo.entity.Product;
import com.imooc.distributedemo.entity.request.AddOrderReq;
import com.imooc.distributedemo.mapper.ProductMapper;
import com.imooc.distributedemo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/list")
    public CommonResult<List<Order>> getList(){
        log.info("url:{}","order/list");
        CommonResult result;
        try{
            List<Order> list = orderService.findAll();
            List<Product> products = productMapper.getAllData();
            result = CommonResult.success(list);
        }catch (Exception e){
            log.error("url:{}","order/list",e);
            result = CommonResult.failed();
        }
        return result;
    }

    @PostMapping("/create")
    public CommonResult<Boolean> createOrder(@RequestBody AddOrderReq addOrderReq){

        CommonResult result;
        try{
            orderService.createOrder(addOrderReq);

            result = CommonResult.success(true);
        }catch (Exception e){
            log.error("error occured!!!",e);
            result = CommonResult.failed();
        }
        return result;

    }
}
